<?php

class QuanLyHocVienController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    protected $tai_khoan;

    public function __construct(Tai_khoan $tai_khoan) {
        $this->tai_khoan = $tai_khoan;
    }

    public function getCourses() {
        $tai_khoans = DB::table('tai_khoans')
                ->leftJoin('enroll_courses', 'enroll_courses.user_id', '=', 'tai_khoans.id')
                ->leftJoin('courses', 'enroll_courses.course_id', '=', 'courses.id')
                ->where('tai_khoans.auth_type', 'student')
                ->select('tai_khoans.id as user_id', 'tai_khoans.fullname', 'tai_khoans.birthday', 'tai_khoans.gender', 'tai_khoans.address', 'tai_khoans.email', 'tai_khoans.phone', 'tai_khoans.active', 'tai_khoans.auth_type', 'enroll_courses.fee_courses', 'courses.id as course_id', 'courses.course_name', 'courses.course_type', 'courses.start_time', 'courses.end_time', 'courses.quantity')
                ->get();
        return View::make('nguoidangky.courses-student', compact('tai_khoans'));
    }

    public function postInfousermanager() {
        $input = Input::all();

        $input_no_pass = array('username' => $input['username'], 'email' => $input['email'], 'birthday' => $input['birthday'], 'email' => $input['email'], 'gender' => $input['gender'], 'phone' => $input['phone'], 'address' => $input['address']);
        $input_pass = array('username' => $input['username'], 'password' => Hash::make($input['password']), 'email' => $input['email'], 'birthday' => $input['birthday'], 'email' => $input['email'], 'gender' => $input['gender'], 'phone' => $input['phone'], 'address' => $input['address']);

        $password_old = DB::table('tai_khoans')->where('username', $input['username'])->pluck('password');
        if ($password_old == $input['password']) {
            DB::table('tai_khoans')->where('username', $input['username'])->update($input_no_pass);
        } else {
            DB::table('tai_khoans')->where('username', $input['username'])->update($input_pass);
        }

        return 1;
    }

    public function postActive() {
        $id = Input::get('id');
        DB::table('tai_khoans')->where('id', '=', $id)->update(array('active' => '1'));
        return 1;
    }

    public function postGetfeecourses() {
        $user_id = Input::get('user_id');
        $course_id = Input::get('course_id');
        $results = DB::table('enroll_courses')
                ->where('user_id', '=', $user_id)
                ->where('course_id', '=', $course_id)
                ->get();
        return $results;
    }

    public function postUpfeecourses() {
        $id = Input::get('id');
        $fee_courses = Input::get('fee_courses');
        // DB::table('enroll_courses')->where('user_id','=',$user_id)
        // 					->where('course_id','=',$course_id)
        // 					->update(array('fee_courses'=>$fee_courses));
        DB::table('enroll_courses')->where('id', '=', $id)
                ->update(array('fee_courses' => $fee_courses));
        return 1;
    }

    public function postFrintliststudent() {
        $course_name = Input::get('course_name');
        if (trim($course_name) == "") {
            $results = DB::table('tai_khoans')
                    ->leftJoin('enroll_courses', 'enroll_courses.user_id', '=', 'tai_khoans.id')
                    ->leftJoin('courses', 'enroll_courses.course_id', '=', 'courses.id')
                    ->select('tai_khoans.fullname', 'tai_khoans.birthday', 'tai_khoans.gender', 'tai_khoans.address', 'tai_khoans.email', 'tai_khoans.phone', 'enroll_courses.fee_courses', 'courses.course_name', 'courses.course_type')
                    ->get();
        } else {
            $results = DB::table('tai_khoans')
                    ->leftJoin('enroll_courses', 'enroll_courses.user_id', '=', 'tai_khoans.id')
                    ->leftJoin('courses', 'enroll_courses.course_id', '=', 'courses.id')
                    ->where('courses.course_name', '=', $course_name)
                    ->select('tai_khoans.fullname', 'tai_khoans.birthday', 'tai_khoans.gender', 'tai_khoans.address', 'tai_khoans.email', 'tai_khoans.phone', 'enroll_courses.fee_courses', 'courses.course_name', 'courses.course_type')
                    ->get();
        }

        $html = '<table border="1" style="width: 100%;border-collapse: collapse;" id="table_data" summary="Code page support in different versions of MS Windows."   ><thead>';
        $html .= '<tr >';
        $html .= '<th style="text-align: center">fullname</th>';
        $html .= '<th style="text-align: center">birthday</th>';
        $html .= '<th style="text-align: center">gender</th>';
        $html .= '<th style="text-align: center">address</th>';
        $html .= '<th style="text-align: center">email</th>';
        $html .= '<th style="text-align: center">phone</th>';
        $html .= '<th style="text-align: center">course_name</th>';
        $html .= '<th style="text-align: center">course_type</th>';
        $html .= '<th style="text-align: center">fee_courses</th>';
        $html .= '</tr>';
        $html .= '</thead>';
        $html .= '<tbody>';
        foreach ($results as $result) {
            $html .= '<tr>';
            $html .= '<td>' . $result->fullname . '</td>';
            $html .= '<td>' . $result->birthday . '</td>';
            $html .= '<td>' . $result->gender . '</td>';
            $html .= '<td>' . $result->address . '</td>';
            $html .= '<td>' . $result->email . '</td>';
            $html .= '<td>' . $result->phone . '</td>';
            $html .= '<td>' . $result->course_name . '</td>';
            $html .= '<td>' . $result->course_type . '</td>';
            $html .= '<td>' . $result->fee_courses . '</td>';
            $html .= '</tr>';
        }
        $html .= '</tbody>';
        $html .= '</table>';

        echo $html;
    }

    public function getSchedules() {
        $results = DB::select('SELECT tai_khoans.username,schedules.event_name,schedules.event_type,tai_khoans.fullname,tai_khoans.birthday,tai_khoans.gender,tai_khoans.address,tai_khoans.email,tai_khoans.phone,schedules.start_time,schedules.end_time,schedules.fee_schedule,tai_khoans.id as user_id,schedules.id as schedule_id FROM tai_khoans INNER JOIN enroll_events ON enroll_events.user_id = tai_khoans.id INNER JOIN schedules ON enroll_events.event_id = schedules.id');
        return View::make('nguoidangky.schedule-student', compact('results'));
    }

    public function postFrintlistschedule() {
        $event_name = Input::get('event_name');
        if (trim($event_name) == "") {
            $results = DB::table('tai_khoans')
                    ->leftJoin('enroll_events', 'enroll_events.user_id', '=', 'tai_khoans.id')
                    ->leftJoin('schedules', 'enroll_events.event_id', '=', 'schedules.id')
                    ->select('tai_khoans.fullname', 'tai_khoans.birthday', 'tai_khoans.gender', 'tai_khoans.address', 'tai_khoans.email', 'tai_khoans.phone', 'schedules.event_name', 'schedules.event_type', 'schedules.fee_schedule')
                    ->get();
        } else {
            $results = DB::table('tai_khoans')
                    ->leftJoin('enroll_events', 'enroll_events.user_id', '=', 'tai_khoans.id')
                    ->leftJoin('schedules', 'enroll_events.event_id', '=', 'schedules.id')
                    ->select('tai_khoans.fullname', 'tai_khoans.birthday', 'tai_khoans.gender', 'tai_khoans.address', 'tai_khoans.email', 'tai_khoans.phone', 'schedules.event_name', 'schedules.event_type', 'schedules.fee_schedule')
                    ->where('schedules.event_name', '=', $event_name)
                    ->get();
        }

        $html = '<table border="1" style="width: 100%;border-collapse: collapse;" id="table_data"><thead>';
        $html .= '<tr>';
        $html .= '<th style="text-align: center">fullname</th>';
        $html .= '<th style="text-align: center">birthday</th>';
        $html .= '<th style="text-align: center">gender</th>';
        $html .= '<th style="text-align: center">address</th>';
        $html .= '<th style="text-align: center">email</th>';
        $html .= '<th style="text-align: center">phone</th>';
        $html .= '<th style="text-align: center">event_name</th>';
        $html .= '<th style="text-align: center">event_type</th>';
        $html .= '<th style="text-align: center">fee_schedule</th>';
        $html .= '</tr>';
        $html .= '</thead>';
        $html .= '<tbody>';
        foreach ($results as $result) {
            $html .= '<tr>';
            $html .= '<td>' . $result->fullname . '</td>';
            $html .= '<td>' . $result->birthday . '</td>';
            $html .= '<td>' . $result->gender . '</td>';
            $html .= '<td>' . $result->address . '</td>';
            $html .= '<td>' . $result->email . '</td>';
            $html .= '<td>' . $result->phone . '</td>';
            $html .= '<td>' . $result->event_name . '</td>';
            $html .= '<td>' . $result->event_type . '</td>';
            $html .= '<td>' . $result->fee_schedule . '</td>';
            $html .= '</tr>';
        }
        $html .= '</tbody>';
        $html .= '</table>';
        echo $html;
    }

    public function getCreateusercourseiframe() {
        $courses = Course::all();
        return View::make('nguoidungtaikhoan.create_course_iframe', compact('courses'));
    }

    public function postCreateusercourseiframe() {
        $id = 0;
        // $input = Input::all();
        $input['fullname'] = Input::get('fullname');
        $input['username'] = Input::get('username');
        $input['password'] = Input::get('password');
        $input['email'] = Input::get('email');
        $input['birthday'] = Input::get('birthday');
        $input['phone'] = Input::get('phone');
        $input['gender'] = Input::get('gender');
        $input['address'] = Input::get('address');
        $course_id = Input::get('course_id');
        $password_old = $input['password'];
        $input['password'] = Hash::make($input['password']);
        $input['auth_type'] = 'student';
        $input['active'] = 0;
        $validation = Validator::make($input, Tai_khoan::$rules_student);
        if ($validation->passes()) {
            $id = DB::table('tai_khoans')->insertGetId($input);
            $fee_courses = DB::table('courses')->where('id', $course_id)->pluck('fee_courses');
            DB::table('enroll_courses')->insert(array('user_id' => $id, 'course_id' => $course_id, 'fee_courses' => $fee_courses));
            return 1;
        }
        return 0;
    }

    public function getCreateuserscheduleiframe() {
        $schedules = Schedule::all();
        return View::make('nguoidungtaikhoan.create_schedule_iframe', compact('schedules'));
    }

    public function postCreateuserscheduleiframe() {
        $id = 0;

        $input['fullname']  = Input::get('fullname');
        $input['username']  = Input::get('username');
        $input['password']  = Input::get('password');
        $input['email']     = Input::get('email');
        $input['birthday']  = Input::get('birthday');
        $input['phone']     = Input::get('phone');
        $input['gender']    = Input::get('gender');
        $input['address']   = Input::get('address');
        $schedule_id        = Input::get('schedule_id');
        $password_old       = $input['password'];
        $input['password']  = Hash::make($input['password']);
        $input['auth_type'] = 'student';
        $input['active']    = 0;
        $validation = Validator::make($input, Tai_khoan::$rules_student);
        if ($validation->passes()) {
            $id = DB::table('tai_khoans')->insertGetId($input);
            DB::table('enroll_events')->insert(array('user_id' => $id, 'schedule_id' => $schedule_id));
            return 1;
        }
        return 0;
    }

    
    public function postGetpagecoursefrint()
    {
        # code...
        $input = Input::all();
        $variable = DB::table('tai_khoans')
                    ->leftJoin('enroll_courses','enroll_courses.user_id', '=', 'tai_khoans.id')
                    ->leftJoin('courses','enroll_courses.course_id', '=', 'courses.id')
                    ->where('tai_khoans.id',$input['user_id'])
                    ->where('courses.id',$input['course_id'])
                    ->select('tai_khoans.fullname','tai_khoans.birthday','tai_khoans.gender','tai_khoans.email','tai_khoans.address','tai_khoans.phone','enroll_courses.fee_courses','courses.course_name','courses.end_time','courses.quantity')
                    ->get();
        
        foreach ($variable as $variable){
            $chuc_nang = new Chuc_nang();
            $tien_chu = $chuc_nang->convert_number_to_words($variable->fee_courses);
            
            $html = '<!DOCTYPE html><html>'
                . '<head><title></title>'
                . '<script src="'.asset("public/admin/js/jquery.min.js").'"></script>'
                . '<script src="'.asset('public/admin/js/jquery-ui-1.10.3.min.js').'" type="text/javascript"></script>'
                . '<script src="'.asset('public/plugins/ckeditor/ckeditor.js').'" type="text/javascript"></script>'
                . '<script src="'.asset('public/plugins/ckeditor/adapters/jquery.js').'" type="text/javascript"></script>'
                . '<style>'
                . '.left-head{font-weight: bold;line-height: 0.5;font-size: 11px;float: left;margin-right: 60px;}'
                . '.right-head{font-size: 22px;font-weight: bold;padding-top: 1px;margin-bottom: -20px;}'
                . '.info-user{float: left;line-height: 0.6;font-size: 16px;}'
                . '.sign-left{margin-left:3px;float: left;line-height: 0.5;}'
                . '.sign-center{float: left;margin-left: 40px;line-height: 0.5;}'
                . '.sign-right{float: right;line-height: 0.5;}'
                . '</style></head>'
                . '<body>'
                . '<div class="left-head"><p>TRƯỜNG ĐẠI HỌC CẦN THƠ</p>'
                . '<p>TRUNG TÂM ĐIỆN TỬ VÀ TIN HỌC</p>'
                . '<p>ĐC: 01 Lý Tự Trọng, P. An Phú, Q. Ninh Kiều, TPCT</p>'
                . '<p>ĐT: 07103.734880 Fax: 07103.830841</p></div>'
                . '<div class="right-head"><p>BIÊN LAI THU TIỀN</p></div>'
                . '<div id="date" contenteditable="true"><p>Ngày .... tháng .... năm 20....</p></div>'
                . '<div class="info-user" id="info" contenteditable="true"><p>Họ tên người nộp tiền: <b>'.$variable->fullname.'</b></p>'
                . '<p>Ngày sinh: '.$variable->birthday.'   Nơi sinh: ..................................................................................................... </p>'
                . '<p>Tên đơn vị: .....................................................................................................................................</p>'
                . '<p>Địa chỉ: '.$variable->address.'</p>'
                . '<p>Hình thức thanh toán: Tiền mặt.                 '.'Số TK:............................................................................................</p><hr/></div>'
                . '<div class="info-user" id="payment" contenteditable="true"><p>Lý do nộp tiền: <i>Học phí/Lệ phí......</i> Lớp:....................... Ngày khai giảng/Thi:....................................</p>'
                . '<p>Phòng học: ....................................... Buổi học:........................................... Thời gian: ............tháng</p>'
                . '<p>Số tiền: '.$variable->fee_courses.' <i>Viết bằng chữ</i>: <i><b>'.$tien_chu.' đồng</b></i> </p></div>'
                . '<div class="sign-left"><p><center><b>Người nộp tiền </b></center></p><p><i><center>(Ký và ghi rõ họ và tên)</center></i></p></div>'
                . '<div class="sign-center"><p><center><b>Người thu tiền</b></center></p><p><i><center>(Ký và ghi rõ họ và tên)</center></i></p></div>'
                . '<div class="sign-right"><p><center><b>Thủ trưởng đơn vị</b></center></p><p><i><center>(Ký, đóng dấu và ghi rõ họ và tên)</center></i></p></div>'
                . '</body>'
                . "<script>CKEDITOR.disableAutoInline = true;CKEDITOR.inline('info',{toolbar: [{ name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ], items: [ 'Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat' ] }]});</script>"
                . "<script>CKEDITOR.disableAutoInline = true;CKEDITOR.inline('date',{toolbar: [{ name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ], items: [ 'Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat' ] }]});</script>"
                . "<script>CKEDITOR.disableAutoInline = true;CKEDITOR.inline('payment',{toolbar: [{ name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ], items: [ 'Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat' ] }]});</script>"
                . '</html>';
        }


    
   
        return $html;
    }


    public function postGetpageschedulefrint()
    {
        # code...
        $input = Input::all();
        $variable = DB::table('tai_khoans')
                    ->leftJoin('enroll_events','enroll_events.user_id', '=', 'tai_khoans.id')
                    ->leftJoin('schedules','enroll_events.event_id', '=', 'schedules.id')
                    ->where('tai_khoans.id',$input['user_id'])
                    ->where('schedules.id',$input['schedule_id'])
                    ->select('tai_khoans.fullname','tai_khoans.birthday','tai_khoans.gender','tai_khoans.email','tai_khoans.address','tai_khoans.phone','schedules.fee_schedule','schedules.event_name','schedules.end_time')
                    ->get();
        
        foreach ($variable as $variable){
            $chuc_nang = new Chuc_nang();
            $tien_chu = $chuc_nang->convert_number_to_words($variable->fee_schedule);
            
            $html = '<!DOCTYPE html><html>'
                . '<head><title></title>'
                . '<script src="'.asset("public/admin/js/jquery.min.js").'"></script>'
                . '<script src="'.asset('public/admin/js/jquery-ui-1.10.3.min.js').'" type="text/javascript"></script>'
                . '<script src="'.asset('public/plugins/ckeditor/ckeditor.js').'" type="text/javascript"></script>'
                . '<script src="'.asset('public/plugins/ckeditor/adapters/jquery.js').'" type="text/javascript"></script>'
                . '</head>'
                . '<style>'
                . '.left-head{font-weight: bold;line-height: 0.5;font-size: 11px;float: left;margin-right: 60px;}'
                . '.right-head{font-size: 22px;font-weight: bold;padding-top: 1px;margin-bottom: -20px;}'
                . '.info-user{float: left;line-height: 0.6;font-size: 16px;}'
                . '.sign-left{margin-left:3px;float: left;line-height: 0.5;}'
                . '.sign-center{float: left;margin-left: 40px;line-height: 0.5;}'
                . '.sign-right{float: right;line-height: 0.5;}'
                . '</style>'
                . '<body>'
                . '<div class="left-head"><p>TRƯỜNG ĐẠI HỌC CẦN THƠ</p>'
                . '<p>TRUNG TÂM ĐIỆN TỬ VÀ TIN HỌC</p>'
                . '<p>ĐC: 01 Lý Tự Trọng, P. An Phú, Q. Ninh Kiều, TPCT</p>'
                . '<p>ĐT: 07103.734880 Fax: 07103.830841</p></div>'
                . '<div class="right-head"><p>BIÊN LAI THU TIỀN</p></div>'
                . '<div id="date" contenteditable="true"><p>Ngày .... tháng .... năm 20....</p></div>'
                . '<div class="info-user" id="info" contenteditable="true"><p>Họ tên người nộp tiền: <b>'.$variable->fullname.'</b></p>'
                . '<p>Ngày sinh: '.$variable->birthday.'   Nơi sinh: ..................................................................................................... </p>'
                . '<p>Tên đơn vị: .....................................................................................................................................</p>'
                . '<p>Địa chỉ: '.$variable->address.'</p>'
                . '<p>Hình thức thanh toán: Tiền mặt.                 '.'Số TK:............................................................................................</p><hr/></div>'
                . '<div class="info-user" id="payment" contenteditable="true"><p>Lý do nộp tiền: <i>Học phí/Lệ phí......</i> Lớp:....................... Ngày khai giảng/Thi:....................................</p>'
                . '<p>Phòng học: ....................................... Buổi học:........................................... Thời gian: ............tháng</p>'
                . '<p>Số tiền: '.$variable->fee_schedule.' <i>Viết bằng chữ</i>: <i><b>'.$tien_chu.' đồng</b></i> </p></div>'
                . '<div class="sign-left"><p><center><b>Người nộp tiền </b></center></p><p><i><center>(Ký và ghi rõ họ và tên)</center></i></p></div>'
                . '<div class="sign-center"><p><center><b>Người thu tiền</b></center></p><p><i><center>(Ký và ghi rõ họ và tên)</center></i></p></div>'
                . '<div class="sign-right"><p><center><b>Thủ trưởng đơn vị</b></center></p><p><i><center>(Ký, đóng dấu và ghi rõ họ và tên)</center></i></p></div>'
                . '</body>'
                . "<script>CKEDITOR.disableAutoInline = true;CKEDITOR.inline('info',{toolbar: [{ name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ], items: [ 'Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat' ] }]});</script>"
                . "<script>CKEDITOR.disableAutoInline = true;CKEDITOR.inline('date',{toolbar: [{ name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ], items: [ 'Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat' ] }]});</script>"
                . "<script>CKEDITOR.disableAutoInline = true;CKEDITOR.inline('payment',{toolbar: [{ name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ], items: [ 'Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat' ] }]});</script>"
                . '</html>';
        }
        return $html;
    }
}
